<?php

namespace App\Kuafu\WaiMai\Command;

use App\Kuafu\WaiMai\Channel\ElmWaiMai;
use App\Kuafu\WaiMai\Channel\MtWaiMai;
use App\Kuafu\WaiMai\Enum\WaiMaiDBEnum;
use App\Kuafu\WaiMai\Model\WaiMaiOrderModel;
use App\Kuafu\WaiMai\Model\WaiMaiShopMappingModel;
use App\Kuafu\WaiMai\Service\BaseWaiMaiOrderService;
use App\Kuafu\WaiMai\Service\ElmWaiMaiOrderService;
use Illuminate\Console\Command;

class WaiMaiAdjustStatus extends Command
{
    private int $date;
    private int $shopId;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'waimai:adjust-status {date} {shopId?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '外卖订单-校正订单状态';

    /**
     * Create a new command instance.
     *
     * @return void//
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command
     *
     * @return int
     */
    public function handle()
    {
        $date   = $this->argument('date');
        $shopId = (int)$this->argument('shopId');

        if (!$date) {
            dd('date必传');
        }

        $this->date   = $date;
        $this->shopId = $shopId;

        $this->elm();
        $this->mt();
    }

    private function elm()
    {
        $sourceType = WaiMaiDBEnum::SOURCE_TYPE_MT_WM;

        WaiMaiShopMappingModel::query()
                              ->when($this->shopId, function ($query) {
                                  $query->when('shopId', $this->shopId);
                              })
                              ->where('sourceType', $sourceType)
                              ->get()
                              ->each(function ($item) use ($sourceType) {
                                  $shopId      = $item->shopId;
                                  $thirdShopId = $item->thirdShopId;

                                  $page = 1;
                                  do {
                                      $data  = ElmWaiMai::getIns($sourceType, $shopId, $thirdShopId)
                                                        ->getAllOrders($page, 50, $this->date)['oriData'];
                                      $list  = $data['list'] ?? [];
                                      $total = $data['total'] ?? 0;

                                      $this->info("elm page:{$page} shopId:{$shopId} total:{$total}");

                                      foreach ($list as $order) {

                                          $orderId = $order['id'];

                                          // 有没有
                                          $db = WaiMaiOrderModel::suffix($shopId)->where('orderId', $orderId)->first();
                                          if (!$db) {
                                              BaseWaiMaiOrderService::getIns($sourceType)
                                                                    ->repairOrder($shopId, $orderId);

                                              $this->info("elm repairOrder shopId:{$shopId} orderId:{$orderId}");
                                              continue;
                                          }

                                          if ($db->orderStatus != ElmWaiMaiOrderService::convertOrderStatus($order['status'])) {
                                              BaseWaiMaiOrderService::getIns($sourceType)
                                                                    ->repairOrderStatus($shopId, $orderId,
                                                                        ['status' => true, 'oriData' => $order]);

                                              $this->info("elm repairStatus shopId:{$shopId} orderId:{$orderId}");
                                              continue;
                                          }
                                      }
                                  } while ($total - 50 * $page > 0);
                              });
    }

    private function mt()
    {
        $sourceType = WaiMaiDBEnum::SOURCE_TYPE_MT_WM;

        WaiMaiShopMappingModel::query()
                              ->when($this->shopId, function ($query) {
                                  $query->when('shopId', $this->shopId);
                              })
                              ->where('sourceType', $sourceType)
                              ->get()
                              ->each(function ($item) use ($sourceType) {
                                  $shopId      = $item->shopId;
                                  $thirdShopId = $item->thirdShopId;

                                  WaiMaiOrderModel::suffix($shopId)
                                                  ->where('sourceType', $sourceType)
                                                  ->where('shopId', $shopId)
                                                  ->whereBetween('createdAt',
                                                      [strtotime($this->date), strtotime($this->date) + 86399])
                                                  ->get()
                                                  ->each(function ($o) use ($sourceType, $shopId, $thirdShopId) {
                                                      $mtOrder = MtWaiMai::getIns($sourceType, $shopId, $thirdShopId)
                                                                         ->getOrderDetail($o->orderId);
                                                      if (!$mtOrder['status']) {
                                                          return;
                                                      }

                                                      if ($mtOrder['data']['orderStatus'] != $o->orderStatus) {
                                                          BaseWaiMaiOrderService::getIns($sourceType)
                                                                                ->repairOrderStatus($shopId,
                                                                                    $o->orderId, $mtOrder);

                                                          $this->info("mt repairStatus shopId:{$shopId} orderId:{$o->orderId}");
                                                      }
                                                  });
                              });
    }
}
